<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 靠右的按钮 -->
      <page-tools>
        <template v-slot:after>
          <el-button type="primary" size="small" @click="addParm(1,'0')">添加权限</el-button>
        </template>
      </page-tools>
      <!-- 表格 -->
      <el-table :data="PermList" border row-key="id" default-expand-all @close="btnCancel">
        <el-table-column align="center" label="名称" prop="name" />
        <el-table-column align="center" label="标识" prop="code" />
        <el-table-column align="center" label="描述" prop="description" />
        <el-table-column align="center" label="操作">
          <template v-slot="{row}">
            <el-button v-if="row.type ===1" type="text" @click="addParm(2,row.id)">添加</el-button>
            <el-button type="text" @click="updatePerm(row.id)">编辑</el-button>
            <el-button type="text" @click="btnDelete(row.id)">删除</el-button>
          </template>
        </el-table-column>

      </el-table>
      <!-- 弹窗 -->
      <el-dialog :title="formData.id?'编辑权限':'新增权限'" :visible="isShowDialog">
        <el-form ref="refParm" :model="formData" :rules="rules" label-width="80px">
          <el-form-item prop="name" label="权限名称">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item prop="code" label="权限标识">
            <el-input v-model="formData.code" />
          </el-form-item>
          <el-form-item prop="description" label="权限描述">
            <el-input v-model="formData.description" />
          </el-form-item>
          <el-form-item label="激活状态">
            <el-switch v-model="formData.enVisible" active-value="1" inactive-value="0" />
          </el-form-item>
        </el-form>
        <template #footer>
          <el-button size="small" type="primary" @click="btnOK">确定</el-button>
          <el-button size="small" @click="btnCancel">取消</el-button>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getPermList, getPermissionDetail, updatePermission, addPermission, delPermission } from '@/api/permission'
import { listToTreeData } from '@/utils/index'
export default {
  data() {
    return {
      PermList: [],
      isShowDialog: false,
      formData: {
        name: '',
        code: '',
        description: '',
        pid: '',
        // nVisible 是否激活权限, 字符串 1 为激活, 字符串 0 为禁用
        enVisible: '1',
        // type 是指权限层级, 第一层为 1 第二层为 2
        type: 1
      },
      rules: {
        name: [{ required: true, message: '该项不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '该项不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '该项不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.PermListFn()
  },
  methods: {
    // 获取数据列表
    async PermListFn() {
      // 使用封装好的方法筛选下级权限
      this.PermList = listToTreeData(await getPermList(), '0')
    },
    // 删除
    async btnDelete(id) {
      await this.$confirm('是否确定要删除？')
      await delPermission(id)
      this.$message.success('操作成功')
      this.PermListFn()
    },
    // 添加
    addParm(type, pid) {
      // 根据点击按钮传d的参不同判断添加层级
      this.formData.type = type
      this.formData.pid = pid
      this.isShowDialog = true
    },
    // 编辑
    async updatePerm(id) {
      this.formData = await getPermissionDetail(id)
      this.isShowDialog = true
    },
    // 确定
    async btnOK() {
      await this.$refs.refParm.validate()
      if (this.formData.id) {
        await updatePermission(this.formData)
      } else {
        await addPermission(this.formData)
      }
      this.formData = {
        name: '',
        code: '',
        description: '',
        pid: '',
        // nVisible 是否激活权限, 字符串 1 为激活, 字符串 0 为禁用
        enVisible: '1',
        // type 是指权限层级, 第一层为 1 第二层为 2
        type: 1
      }
      this.isShowDialog = false
      this.$message.success('操作成功')
      this.PermListFn()
    },
    // 取消
    async btnCancel() {
      this.formData = {
        name: '',
        code: '',
        description: '',
        pid: '',
        // nVisible 是否激活权限, 字符串 1 为激活, 字符串 0 为禁用
        enVisible: '1',
        // type 是指权限层级, 第一层为 1 第二层为 2
        type: 1
      }
      await this.$refs.refParm.resetFields()

      this.isShowDialog = false
    }
  }
}
</script>

<style>

</style>
